<?php
class NamhocsController extends AppController {

	var $name = 'Namhocs';

	function index() {
		$this->Namhoc->recursive = 0;
		$this->set('namhocs', $this->paginate());
	}
    
    function thuchi() {
		$this->Namhoc->recursive = 0;
		$this->set('namhocs', $this->paginate());
        //debug($this->paginate(array('id'=>'asc')));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid namhoc', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('namhoc', $this->Namhoc->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Namhoc->create();
			if ($this->Namhoc->save($this->data))
			{
				$nh = $this->data['Namhoc']['namhoc'];
				$namhoc = split('-',$nh);
				for($i=8;$i<=12;$i++)
				{
					$thang= $i.'-'.$namhoc[0];
					$str = "insert into thangs values (NULL,".$this->Namhoc->id.",'".$thang."',NULL)";
					$this->Namhoc->query($str);
				}
				for($i=1;$i<=7;$i++)
				{
					$thang= $i.'-'.$namhoc[1];
					$str = "insert into thangs values (NULL,".$this->Namhoc->id.",'".$thang."',NULL)";
					$this->Namhoc->query($str);
				}
				$this->Session->setFlash(__('Thông in đã lưu', true),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu. Vui lòng làm lại.', true),'error');
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid namhoc', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Namhoc->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Namhoc->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for namhoc', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Namhoc->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true),'error');
		$this->redirect(array('action' => 'index'));
	}
}
